home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / performance / snoopers / cpuload2 / src / cpuload.c
Encoding:
C/C++ Source or Header  |  1997-11-20  |  5.1 KB  |  250 lines

  1. #include <intuition/intuitionbase.h>
  2. #include <exec/nodes.h>
  3. #include <exec/tasks.h>
  4. #include <libraries/dos.h>
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include <exec/libraries.h>
  11. #include <dos/dos.h>
  12.  
  13. struct Task *task,*met;
  14. char *taskname = "CPU_TASK";
  15.  
  16. struct timerequest *tr;
  17. struct MsgPort *tport;
  18. struct Message *msg;
  19. struct Window *CustWindow;
  20. struct IntuitionBase *IntuitionBase;
  21. struct GfxBase *GfxBase;
  22. struct Library *IconBase;
  23. struct Library *CxBase;
  24.  
  25. #define rp CustWindow->RPort
  26.  
  27. struct NewWindow BlankWindow=
  28. {
  29.     0,0,130,50,1,0,
  30.     CLOSEWINDOW | NEWSIZE,
  31.     WINDOWSIZING | ACTIVATE | GIMMEZEROZERO | SMART_REFRESH | WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH,
  32.     0,0,
  33.     "CPULoad",
  34.     0,0,70,20,0,0,
  35.     PUBLICSCREEN
  36. };
  37.  
  38. unsigned int maximum,aktuell,cnt,quit=0;
  39.  
  40. /*    System structure name    */
  41.  
  42. #define UTILITY_NAME "CPU_PROC"
  43.  
  44. /*    allocate the timer    */
  45.  
  46. struct timerequest *create_timer(ULONG unit)
  47. {
  48.     LONG error;
  49.     struct MsgPort *timerport;
  50.     struct timerequest *timermsg;
  51.  
  52.     if ((timerport=(struct MsgPort *)CreatePort(0,0))==NULL) exit(1);
  53.     if ((timermsg=(struct timerequest *)CreateExtIO(timerport,sizeof(struct timerequest)))==NULL) exit(1);
  54.     if ((error=OpenDevice(TIMERNAME,unit,(struct IORequest *)timermsg,0))!=0) exit(1);
  55.     return(timermsg);
  56. }
  57.     
  58. /*    Läuft als Task    */
  59.  
  60. druck()
  61. {
  62.     struct IntuiMessage *message;    /*    Windowclose    */
  63.     struct IntuiMessage *GetMsg();
  64.     struct timeval updateval;    /*    1/10 Sekunde    */
  65.     char name[256];            /*    PubScreen    */
  66.     int n;
  67.     unsigned int sum;        /*    Summand der freien Zyklen    */
  68.     unsigned int height,width,right,bottom;
  69.     unsigned int int_pix_per_load;        /*    Skalierung    */
  70.     int whitepen,blackpen;
  71.     struct Screen *PubScreen;
  72.     short pen,minpen,maxpen;
  73.     unsigned short RGB;
  74.     int class;
  75.  
  76.     geta4();
  77.  
  78. /*    init timer    */
  79.  
  80.     tr=create_timer(UNIT_MICROHZ);
  81.  
  82.     updateval.tv_secs=0;
  83.     updateval.tv_micro=100000;    /*    1/10 secs    */
  84.  
  85.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
  86.     GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
  87.  
  88.     GetDefaultPubScreen(name);
  89.     PubScreen=(struct Screen *)LockPubScreen(name);
  90.     UnlockPubScreen(0L,PubScreen);
  91.  
  92.     BlankWindow.MaxHeight=PubScreen->Height;
  93.     BlankWindow.MaxWidth=PubScreen->Width;
  94.  
  95.     CustWindow=(struct Window *)OpenWindow(&BlankWindow);
  96.  
  97. /*    Farbgebung aus der ColorMap bestimmen (SunClock-Methode)    */
  98.  
  99.     whitepen=1;
  100.     blackpen=0;
  101.     maxpen=0x000;
  102.     minpen=0xfff;
  103.  
  104.     for (n=0; n<1<<PubScreen->BitMap.Depth; n++)
  105.     {
  106.         RGB=GetRGB4(PubScreen->ViewPort.ColorMap,n);
  107.  
  108.         pen=(RGB & 0x00f) + ((RGB & 0x0f0) >> 4) + ((RGB & 0xf00) >> 8);
  109.         if (pen < minpen)
  110.         {
  111.             minpen = pen;
  112.             blackpen = n;
  113.         }
  114.         if (pen > maxpen)
  115.         {
  116.             maxpen = pen;
  117.             whitepen = n;
  118.         }
  119.     }
  120.  
  121.     height=CustWindow->GZZHeight;
  122.     width=CustWindow->GZZWidth;
  123.     right=width-1;
  124.     bottom=height-1;
  125.  
  126.     SetAPen(rp,whitepen);
  127.     RectFill(rp,0,0,right,bottom);
  128.  
  129. /*    FOREVER    */
  130.  
  131.     while (1)
  132.     {
  133.         sum=0;
  134.  
  135.         for (n=0; n<10; n++)
  136.         {
  137.             cnt=0;
  138.             tr->tr_node.io_Command=TR_ADDREQUEST;
  139.             tr->tr_time=updateval;
  140.             DoIO((struct IORequest *)tr);
  141.             aktuell=cnt;
  142.             sum+=aktuell;
  143.         }
  144.  
  145.         sum/=10;    /*    Average    */
  146.  
  147. /*    Verschiebe Bild nach links    */
  148.  
  149.         height=CustWindow->GZZHeight;
  150.         width=CustWindow->GZZWidth;
  151.         right=width-1;
  152.         bottom=height-1;
  153.  
  154.         int_pix_per_load=(height<<16)/maximum;
  155.  
  156.         ScrollRaster(rp,1,0,0,0,width,height);
  157.  
  158. /*    Jede Sekunde Ausdruck    */
  159.  
  160.         SetAPen(rp,blackpen);
  161.         Move(rp,right,bottom);
  162.  
  163.         Draw(rp,right,(sum*int_pix_per_load)>>16);
  164.         if (rp->cp_y)
  165.         {
  166.             SetAPen(rp,whitepen);
  167.             Draw(rp,right,0);
  168.         }
  169.  
  170.         while (message=GetMsg(CustWindow->UserPort))
  171.         {
  172.             class=message->Class;
  173.             ReplyMsg(message);
  174.  
  175.             if (class==CLOSEWINDOW)
  176.             {
  177.                 Signal(met,SIGBREAKF_CTRL_D);
  178.             }
  179.             else if (class==NEWSIZE)
  180.             {
  181.                 SetAPen(rp,whitepen);
  182.                 RectFill(rp,0,0,right,bottom);
  183.             }    
  184.         }
  185.     }
  186. }
  187.  
  188. /* Run this as a process. It aborts when it detects a CTRL_D signal */
  189.  
  190. main(int argc, char **argv)
  191. {
  192.     extern struct Task *FindTask(char *);
  193.     WORD oldpri;
  194.     char *oldname;
  195.     int i;
  196.     UBYTE **ttypes;
  197.  
  198. /*    switch off CTRL_C handling    */
  199.  
  200.     if (met=FindTask(UTILITY_NAME)) exit(1);
  201.  
  202.     if (IconBase=(struct Library *)OpenLibrary("icon.library",37L))
  203.     {
  204.         if (CxBase=(struct Library *)OpenLibrary("icon.library",37L))
  205.         {
  206.             if ((ttypes=ArgArrayInit(argc,argv)))
  207.             {
  208.                 BlankWindow.LeftEdge=ArgInt(ttypes,"XPOS",0);
  209.                 BlankWindow.TopEdge=ArgInt(ttypes,"YPOS",0);
  210.                 BlankWindow.Width=ArgInt(ttypes,"XSIZE",130);
  211.                 BlankWindow.Height=ArgInt(ttypes,"YSIZE",50);
  212.                 CloseLibrary(CxBase);
  213.             }
  214.         }
  215.         CloseLibrary(IconBase);
  216.     }
  217.  
  218.     met=FindTask(NULL);            /*    Finde mich selber    */
  219.     oldpri=met->tc_Node.ln_Pri;        /*    Save old priority    */
  220.     oldname=met->tc_Node.ln_Name;        /*    Save old name    */
  221.  
  222.     met->tc_Node.ln_Name=UTILITY_NAME;
  223.     met->tc_Node.ln_Pri=126;    /*    Raise priority for calibration    */
  224.  
  225.     maximum=1000000;
  226.  
  227.     task=CreateTask(taskname,127,druck,4096);
  228.  
  229.     aktuell=0;
  230.     while(!aktuell) cnt++;
  231.     maximum=aktuell;
  232.  
  233.     met->tc_Node.ln_Pri=-127;
  234.  
  235.     while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  236.  
  237.     met->tc_Node.ln_Pri=oldpri;    /*    Change priority back    */
  238.     met->tc_Node.ln_Name=oldname;    /*    Change name back    */
  239.  
  240.     Forbid();
  241.     DeleteTask(task);
  242.     Permit();
  243.  
  244.     CloseWindow(CustWindow);
  245.     CloseLibrary(IntuitionBase);
  246.     CloseLibrary(GfxBase);
  247.  
  248.     exit(1);
  249. }
  250.